Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
JavaScript Higher Order Function keeps returning newArray as [Nan, Nan, Nan] when it should be numbers

I've been working on a problem that involves higher order functions and callbacks. The callback function is below.

**const addTwo = num => num + 2;**

I'm trying to pass this callback into the higher order function below.

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(newArray[i]))
  }
  return newArray;
};**

The variable "newArray," inside of the map function should be returned as an array of 3 different numbers added by two. The arguments for the map function's parameters are below within a console log.

**console.log(map([1, 2, 3], addTwo));**

The addTwo function should become the argument of the callback parameter within the map function, this should make every element in the above array be added by two. I keep seeing [NaN, NaN, NaN] in the console and I'm not quite sure why every number in the index isn't being added by two.

I would appreciate the help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are pushing the values from the new array, when it should be the argument instead

**const map = (array, callback) => {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(callback(array[i]))
  }
  return newArray;
};**
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!